#!/usr/bin/env node
import { readFile, writeFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { glob } from 'glob';

const __dirname = dirname(fileURLToPath(import.meta.url));
const esmDir = join(__dirname, '../lib/esm');

const files = await glob('**/*.mjs', { cwd: esmDir });
for (const f of files) {
  const full = join(esmDir, f);
  let code = await readFile(full, 'utf8');
  // ① 补扩展名：from './foo' -> from './foo.mjs'
  code = code.replace(/from\s+(['"])(\.\/[^'"]+)(?!\.\w+)\1/g, "from '$2.mjs'");
  await writeFile(full, code);
}
console.log('✔ ESM imports fixed');